package com.infitecs.eyas.quality.service.Imp;

import com.infitecs.eyas.boiler.dao.EfficiencyDao;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.combustion.dao.BurnDao;
import com.infitecs.eyas.combustion.dao.ChimneyDao;
import com.infitecs.eyas.combustion.dao.CleanDao;
import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.model.CalculateChimney;
import com.infitecs.eyas.combustion.model.CalculateClean;
import com.infitecs.eyas.combustion.service.BurnService;
import com.infitecs.eyas.combustion.service.ChimneyService;
import com.infitecs.eyas.combustion.service.CleanService;
import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.expression.dao.ExpressionDao;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.mill.dao.MillSelectDao;
import com.infitecs.eyas.mill.dao.PipeCalculateDao;
import com.infitecs.eyas.mill.model.MillSelect;
import com.infitecs.eyas.mill.model.PipeCalculate;
import com.infitecs.eyas.mill.service.MillSelectService;
import com.infitecs.eyas.mill.service.PipeCalculateService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.quality.dao.AshDao;
import com.infitecs.eyas.quality.dao.BurstDao;
import com.infitecs.eyas.quality.dao.ElementDao;
import com.infitecs.eyas.quality.dao.IndustryDao;
import com.infitecs.eyas.quality.model.Ash;
import com.infitecs.eyas.quality.model.Burst;
import com.infitecs.eyas.quality.service.AshService;
import com.infitecs.eyas.quality.service.BurstService;
import com.infitecs.eyas.runcoalbin.dao.CoalBunkerDao;
import com.infitecs.eyas.runcoalbin.model.CoalBunker;
import com.infitecs.eyas.runcoalbin.service.CoalBunkerService;
import com.infitecs.eyas.util.DoubleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xidea.el.Expression;
import org.xidea.el.impl.ExpressionImpl;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: chenkaiyu
 * Date: 15-4-25
 * Time: 上午7:06
 * To change this template use File | Settings | File Templates.
 */
@Service("ashService")
@Transactional
public class AshServiceImp implements AshService {
    @Autowired
    private AshDao ashDao;
    @Autowired
    private ExpressionDao expressionDao;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private BurstService burstService;
    @Autowired
    private BurstDao burstDao;
    @Autowired
    private EfficiencyDao efficiencyDao;
    @Autowired
    private EfficiencyService efficiencyService;

    @Autowired
    private CoalBunkerDao coalBunkerDao;
    @Autowired
    private CoalBunkerService coalBunkerService;
    @Autowired
    private BurnService burnService;
    @Autowired
    private BurnDao burnDao;
    @Autowired
    private CleanService cleanService;
    @Autowired
    private CleanDao cleanDao;
    @Autowired
    private ChimneyDao chimneyDao;
    @Autowired
    private ChimneyService chimneyService;
    @Autowired
    private MillSelectDao millSelectDao;
    @Autowired
    private MillSelectService millSelectService;
    @Autowired
    private PipeCalculateService pipeCalculateService;
    @Autowired
    private PipeCalculateDao pipeCalculateDao;
    @Override
    public Double calculateE48(int caseId) {
        Ash ash = ashDao.getAshByPowerCaseId(caseId);
        double Sio2 = ash.getSio2();
        double Al2O3 = ash.getAl2o3();
        double Fe2O3 = ash.getFe2o3();
        double Cao = ash.getCao();
        double TiO2 = ash.getTio2();
        double MgO = ash.getMgo();
        double SO3 = ash.getSo3();
        double k2O = ash.getK2o();
        double Na2O = ash.getNa2o();
        double other = 100 - Sio2 - Al2O3 - Fe2O3 - Cao - TiO2 - MgO - SO3 - k2O - Na2O;
        ash.setOther(other);
        ashDao.updateE48(ash);
        other = DoubleUtil.getInstance().get3Double(other);
        return other;
    }

    @Override
    public Double calculateTotal(int caseId) {
        Ash ash = ashDao.getAshByPowerCaseId(caseId);
        double Sio2 = ash.getSio2();
        double Al2O3 = ash.getAl2o3();
        double Fe2O3 = ash.getFe2o3();
        double Cao = ash.getCao();
        double TiO2 = ash.getTio2();
        double MgO = ash.getMgo();
        double SO3 = ash.getSo3();
        double k2O = ash.getK2o();
        double Na2O = ash.getNa2o();
        double Other = ash.getOther();
        double total = Sio2 + Al2O3 + Fe2O3 + Cao + TiO2 + MgO + SO3 + k2O + Na2O + Other;
        ash.setTotal(total);
        ashDao.updateE49(ash);
        total = DoubleUtil.getInstance().get2Double(total);
        return total;
    }

    @Override
    public Double calculateE57(int caseId) {
        Ash ash = ashDao.getAshByPowerCaseId(caseId);
        HashMap<String, Double> map = new HashMap<String, Double>();
        map.put("E53", ash.getDt());
        map.put("E55", ash.getHt());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_10);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        Object result = expression.evaluate(map);
        double E57 = Double.parseDouble(result.toString());
        ash.setRt(E57);
        ashDao.updateE57(ash);
        E57 = DoubleUtil.getInstance().get2Double(E57);
        return E57;
    }

    @Override
    public Double calculateRz(int caseId) {
        Ash ash = ashDao.getAshByPowerCaseId(caseId);
        double E54 = ash.getSt();
        ash = ashDao.getAshByPowerCaseId(ash.getPowerCaseId());
        HashMap<String, Double> map1 = new HashMap<String, Double>();
        HashMap<String, Double> map2 = new HashMap<String, Double>();
        HashMap<String, Double> map3 = new HashMap<String, Double>();
        HashMap<String, Double> map4 = new HashMap<String, Double>();
        Formula f1 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_11);
        Formula f2 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_12);
        Formula f3 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_13);
        Formula f4 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_14);
        Expression expression1 = new ExpressionImpl(f1.getFormulaDescription());
        Expression expression2 = new ExpressionImpl(f2.getFormulaDescription());
        Expression expression3 = new ExpressionImpl(f3.getFormulaDescription());
        Expression expression4 = new ExpressionImpl(f4.getFormulaDescription());
        map1.put("E35", ash.getFe2o3());
        map1.put("E36", ash.getCao());
        map1.put("E38", ash.getMgo());
        map1.put("E41", ash.getNa2o());
        map1.put("E40", ash.getK2o());
        map1.put("E33", ash.getSio2());
        map1.put("E34", ash.getAl2o3());
        map1.put("E37", ash.getTio2());
        Object E82 = expression1.evaluate(map1);
        double num_E82 = Double.parseDouble(E82.toString());
        map2.put("E33", ash.getSio2());
        map2.put("E34", ash.getAl2o3());
        Object E83 = expression2.evaluate(map2);
        double num_E83 = Double.parseDouble(E83.toString());
        map3.put("E33", ash.getSio2());
        map3.put("E35", ash.getFe2o3());
        map3.put("E36", ash.getCao());
        map3.put("E38", ash.getMgo());
        Object E84 = expression3.evaluate(map3);
        double num_E84 = Double.parseDouble(E84.toString());
        map4.put("E82", num_E82);
        map4.put("E83", num_E83);
        map4.put("E84", num_E84);
        map4.put("E54", E54);
        Object E85 = expression4.evaluate(map4);
        double num_E85 = Double.parseDouble(E85.toString());
        ash.setR2(num_E85);
        ashDao.updateRz(ash);
        num_E85 = DoubleUtil.getInstance().get3Double(num_E85);
        return num_E85;
    }

    @Override
    public void saveTheValueOfSiO2(double sio2, int caseId) {
        sio2 = DoubleUtil.getInstance().get2Double(sio2);
        ashDao.updateSio2(sio2, caseId);
    }

    @Override
    public void saveTheValueOfAl2O3(double al2o3, int caseId) {
        al2o3 = DoubleUtil.getInstance().get2Double(al2o3);
        ashDao.updateAl2O3(al2o3, caseId);
    }

    @Override
    public void saveTheValueOfFe2O3(double fe2o3, int caseId) {
        fe2o3 = DoubleUtil.getInstance().get2Double(fe2o3);
        ashDao.updateFe2O3(fe2o3, caseId);
    }

    @Override
    public void saveTheValueOfCaO(double cao, int caseId) {
        cao = DoubleUtil.getInstance().get2Double(cao);
        ashDao.updateCaO(cao, caseId);
    }

    @Override
    public void saveTheValueOfTiO2(double tio2, int caseId) {
        tio2 = DoubleUtil.getInstance().get2Double(tio2);
        ashDao.updateTiO2(tio2, caseId);
    }

    @Override
    public void saveTheValueOfMgO(double mgo, int caseId) {
        mgo = DoubleUtil.getInstance().get2Double(mgo);
        ashDao.updateMgO(mgo, caseId);
    }

    @Override
    public void saveTheValueOfSO3(double so3, int caseId) {
        so3 = DoubleUtil.getInstance().get2Double(so3);
        ashDao.updateSo3(so3, caseId);
    }

    @Override
    public void saveTheValueOfK2O(double k2o, int caseId) {
        k2o = DoubleUtil.getInstance().get2Double(k2o);
        ashDao.updateK2O(k2o, caseId);
    }

    @Override
    public void saveTheValueOfNa2O(double na2o, int caseId) {
        na2o = DoubleUtil.getInstance().get2Double(na2o);
        ashDao.updateNa2O(na2o, caseId);
    }

    @Override
    public void saveTheValueOfOther(double other, int caseId) {
        other = DoubleUtil.getInstance().get2Double(other);
        ashDao.updateOther(other, caseId);
    }

    @Override
    public void saveTheValueOfDT(double dt, int caseId) {
        dt = DoubleUtil.getInstance().get2Double(dt);
        ashDao.updateDT(dt, caseId);
    }

    @Override
    public void saveTheValueOfST(double st, int caseId) {
        st = DoubleUtil.getInstance().get2Double(st);
        ashDao.updateST(st, caseId);
    }

    @Override
    public void saveTheValueOfHT(double ht, int caseId) {
        ht = DoubleUtil.getInstance().get2Double(ht);
        ashDao.updateHT(ht, caseId);
    }

    @Override
    public void saveTheValueOfFT(double ft, int caseId) {
        ft = DoubleUtil.getInstance().get2Double(ft);
        ashDao.updateFT(ft, caseId);
    }

    @Override
    public Ash getAsh(int caseId) {
        return ashDao.getAshByPowerCaseId(caseId);
    }

    @Override
    public void ashSave(Ash ash) {
        ashSaveData(ash);
        //更新磨损腐蚀爆炸性
        Burst oldBurst = burstDao.getBurstByPowerCaseId(ash.getPowerCaseId());
        if (oldBurst == null) return;
        burstService.boomSaveData(oldBurst);
        OperateField operateField = operateFieldDao.getOperateField(ash.getPowerCaseId());
        //更新锅炉效率及燃煤量
        Efficiency oldEfficiency = efficiencyDao.getEfficiencyByPowerCaseId(ash.getPowerCaseId());
        if (oldEfficiency != null) {
            oldEfficiency.setE52State(operateField.getE52State());
            oldEfficiency.setE89State(operateField.getE89State());
            oldEfficiency.setE90State(operateField.getE90State());
            oldEfficiency.setE92State(operateField.getE92State());
            oldEfficiency.setE93State(operateField.getE93State());
            oldEfficiency.setE94State(operateField.getE94State());
            oldEfficiency.setE95State(operateField.getE95State());
            efficiencyService.efficiencySaveData(oldEfficiency);
        }
        //更新原煤仓
        CoalBunker oldCoalBunker = coalBunkerDao.getCoalBunker(ash.getPowerCaseId());
        //更新原煤仓信息
        if (oldCoalBunker != null) {
            oldCoalBunker.setE9State(operateField.getE9State());
            coalBunkerService.designSaveData(oldCoalBunker);
        }
        //更新燃烧
        CalculateBurn calculateBurn = burnDao.getCalculateBurnByCaseId(ash.getPowerCaseId());
        if (calculateBurn != null) {
            calculateBurn.setYyglState(operateField.getYyglState());
            calculateBurn.setE54State(operateField.getE54State());
            calculateBurn.setE88State(operateField.getE88State());
            calculateBurn.setE122State(operateField.getE122State());
            calculateBurn.setE155State(operateField.getE155State());
            calculateBurn.setE171State(operateField.getE171State());
            burnService.burnSaveData(calculateBurn);
        }
        //更新烟气净化系统
        CalculateClean calculateClean = cleanDao.getCalculateClean(ash.getPowerCaseId());
        if (calculateClean != null) {
            calculateClean.setE221State(operateField.getE221State());
            calculateClean.setE222State(operateField.getE222State());
            calculateClean.setE231State(operateField.getE231State());
            calculateClean.setE241State(operateField.getE241State());
            calculateClean.setE242State(operateField.getE242State());
            calculateClean.setE248State(operateField.getE248State());
            calculateClean.setE258State(operateField.getE258State());
            cleanService.cleanDataSave(calculateClean);
        }
        //更新烟囱出口直径计算
        CalculateChimney calculateChimney = chimneyDao.getCalculateChimney(ash.getPowerCaseId());
        if (calculateChimney != null) {
            calculateChimney.setE314State(operateField.getE314State());
            calculateChimney.setE315State(operateField.getE315State());
            calculateChimney.setE316State(operateField.getE316State());
            calculateChimney.setCaseId(operateField.getCaseId());
            chimneyService.chimneySaveData(calculateChimney);
        }

        //更新磨煤机选择
        MillSelect millSelect = millSelectDao.getMillSelect(ash.getPowerCaseId());
        if (millSelect != null) {
            millSelect.setMyglState(operateField.getMyglState());
            millSelect.setE12State(operateField.getE12State());
            millSelect.setE74State(operateField.getE74State());
            millSelect.setE75State(operateField.getE75State());
            millSelect.setModelState(operateField.getModelState());
            millSelectService.chooseDataSave(millSelect);
        }
        //更新煤粉管道计算
        PipeCalculate pipeCalculate = pipeCalculateDao.getPipeCalculate(ash.getPowerCaseId());
        if (pipeCalculate != null) {
            pipeCalculateService.pipelineDataSave(pipeCalculate);
        }
    }

    @Override
    public void ashSaveData(Ash ash) {
        //计算其他
        if ("0".equals(ash.getOtherState())) {
            double other = calculateE48(ash);
            ash.setOther(other);
        }
        //计算合计
        ash.setTotal(calculateTotal(ash));
        //计算结渣特性判断Rt
        ash.setRt(calculateE57(ash));
        //计算结焦指数Rz
        ash.setR2(calculateRz(ash));

        Ash oldAsh = ashDao.getAshByPowerCaseId(ash.getPowerCaseId());
        //校验新增或者编辑
        if (oldAsh == null) {
            ashDao.addAsh(ash);
        } else {
            ashDao.updateAsh(ash);
        }
        //更新只读或者修改状态
        OperateField operateField = new OperateField();
        operateField.setCaseId(ash.getPowerCaseId());
        operateField.setOtherState(ash.getOtherState());
        operateFieldDao.updateOperateFieldAsh(operateField);
    }

    public Double calculateE48(Ash ash) {
        double Sio2 = ash.getSio2();
        double Al2O3 = ash.getAl2o3();
        double Fe2O3 = ash.getFe2o3();
        double Cao = ash.getCao();
        double TiO2 = ash.getTio2();
        double MgO = ash.getMgo();
        double SO3 = ash.getSo3();
        double k2O = ash.getK2o();
        double Na2O = ash.getNa2o();
        double other = 100 - Sio2 - Al2O3 - Fe2O3 - Cao - TiO2 - MgO - SO3 - k2O - Na2O;
        return DoubleUtil.getInstance().get3Double(other);
    }

    public Double calculateTotal(Ash ash) {
        double Sio2 = ash.getSio2();
        double Al2O3 = ash.getAl2o3();
        double Fe2O3 = ash.getFe2o3();
        double Cao = ash.getCao();
        double TiO2 = ash.getTio2();
        double MgO = ash.getMgo();
        double SO3 = ash.getSo3();
        double k2O = ash.getK2o();
        double Na2O = ash.getNa2o();
        double Other = ash.getOther();
        double total = Sio2 + Al2O3 + Fe2O3 + Cao + TiO2 + MgO + SO3 + k2O + Na2O + Other;
        return DoubleUtil.getInstance().get2Double(total);
    }

    public Double calculateE57(Ash ash) {
        HashMap<String, Double> map = new HashMap<String, Double>();
        map.put("E53", ash.getDt());
        map.put("E55", ash.getHt());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_10);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        Object result = expression.evaluate(map);
        double E57 = Double.parseDouble(result.toString());
        return DoubleUtil.getInstance().get2Double(E57);
    }

    public Double calculateRz(Ash ash) {
        HashMap<String, Double> map1 = new HashMap<String, Double>();
        HashMap<String, Double> map2 = new HashMap<String, Double>();
        HashMap<String, Double> map3 = new HashMap<String, Double>();
        HashMap<String, Double> map4 = new HashMap<String, Double>();
        Formula f1 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_11);
        Formula f2 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_12);
        Formula f3 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_13);
        Formula f4 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_14);
        Expression expression1 = new ExpressionImpl(f1.getFormulaDescription());
        Expression expression2 = new ExpressionImpl(f2.getFormulaDescription());
        Expression expression3 = new ExpressionImpl(f3.getFormulaDescription());
        Expression expression4 = new ExpressionImpl(f4.getFormulaDescription());
        map1.put("E35", ash.getFe2o3());
        map1.put("E36", ash.getCao());
        map1.put("E38", ash.getMgo());
        map1.put("E41", ash.getNa2o());
        map1.put("E40", ash.getK2o());
        map1.put("E33", ash.getSio2());
        map1.put("E34", ash.getAl2o3());
        map1.put("E37", ash.getTio2());
        Object E82 = expression1.evaluate(map1);
        double num_E82 = Double.parseDouble(E82.toString());
        map2.put("E33", ash.getSio2());
        map2.put("E34", ash.getAl2o3());
        Object E83 = expression2.evaluate(map2);
        double num_E83 = Double.parseDouble(E83.toString());
        map3.put("E33", ash.getSio2());
        map3.put("E35", ash.getFe2o3());
        map3.put("E36", ash.getCao());
        map3.put("E38", ash.getMgo());
        Object E84 = expression3.evaluate(map3);
        double num_E84 = Double.parseDouble(E84.toString());
        map4.put("E82", num_E82);
        map4.put("E83", num_E83);
        map4.put("E84", num_E84);
        map4.put("E54", ash.getSt());
        Object E85 = expression4.evaluate(map4);
        double num_E85 = Double.parseDouble(E85.toString());
        return DoubleUtil.getInstance().get3Double(num_E85);
    }

}
